const Router = require('koa-router');
const CourseTypeRouter = new Router();
const CourseTypeService = require('../service/CourseTypeService');
const R = require('../utils/R');
const Validators = require('../utils/validators');

CourseTypeRouter.prefix('/api/v1/coursetype');

CourseTypeRouter.get('/list',async (ctx, next) => {
    let data = await CourseTypeService.findAll();

    ctx.body = R.okData(data)
})

CourseTypeRouter.post('/save', async (ctx, next) => {
    let typeObj = ctx.request.body;
    Validators.isNull(typeObj.categoryName);
    await CourseTypeService.saveCourseType(typeObj);

    ctx.body = R.okMsg('添加成功');
})

CourseTypeRouter.post('/delete', async (ctx, next) => {
    let typeObj = ctx.request.body;
    Validators.isNull(typeObj.categoryId, 'categoryId');
    await CourseTypeService.deleteCourseType(typeObj.categoryId);

    ctx.body = R.okMsg('删除成功')
})

CourseTypeRouter.post('/update', async (ctx, next) => {
    let obj = ctx.request.body;
    Validators.isNull(typeObj.categoryId, 'categoryId').isNull(typeObj.categoryName);
    await CourseTypeService.updateCourseType(obj);
    ctx.body = R.okMsg('修改成功')
})

module.exports = CourseTypeRouter